/*
 * Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.i360day.invoker.socket.handler;

import com.i360day.invoker.socket.SimpleWebSocketTemplate;
import jakarta.websocket.CloseReason;
import jakarta.websocket.Endpoint;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author: liju.z
 * @date: 2024/4/1 13:04
 */
public class SimpleWebSocketEndpoint extends Endpoint {
    private final Logger logger = LoggerFactory.getLogger(SimpleWebSocketEndpoint.class);
    private final SimpleWebSocketTemplate webSocketTemplate;
    public SimpleWebSocketEndpoint(SimpleWebSocketTemplate webSocketTemplate) {
        this.webSocketTemplate = webSocketTemplate;
    }

    @Override
    public void onOpen(Session session, EndpointConfig config) {
        //设置最大空闲超时
        session.setMaxIdleTimeout(30 * 1000);
        //设置最大二进制消息缓冲区大小
        session.setMaxBinaryMessageBufferSize(1024 * 8);
        //设置最大文本消息缓冲区大小
        session.setMaxTextMessageBufferSize(1024 * 8);
        //通知
        webSocketTemplate.addSocket(session);
        //监听事件
        session.addMessageHandler(new SimpleMessageByteBufferHandler(this, session, webSocketTemplate));
        //ping
        session.addMessageHandler(new SimpleMessagePingHandler(session));
    }

    @Override
    public void onClose(Session session, CloseReason closeReason) {
        try {
            logger.info("{} onClose", session.getRequestURI());
            if (session.isOpen()) {
                session.close(closeReason);
            }
        } catch (IOException ex) {
            logger.error("{} onClose error {}", session.getRequestURI(), ex);
        } finally {
            webSocketTemplate.removeSocket(session);
        }
    }

    @Override
    public void onError(Session session, Throwable thr) {
        logger.error("{} client {} onError {} ", session.getRequestURI(), session.getUserProperties().get("Client-Id"), thr.getMessage());
    }
}
